package net.cl.support.codec;

import java.security.MessageDigest;

import net.cl.utils.ByteUtil;

/**
 * Message-digest Algorithm 5 信息-摘要算法
 * 
 * @author caol64@gmail.com
 */
public class MD5 {

	/**
	 * MD5校验(32位)
	 */
	public static String encode(byte[] in) throws CodecException {
		if (in == null)
			return null;
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.update(in);
			byte[] b = md5.digest();
			return ByteUtil.bytes2HexString(b);
		} catch (Exception e) {
			throw new CodecException(e);
		}
	}

	/**
	 * MD5校验(32位)
	 */
	public static String encode(String in) throws CodecException {
		if (in == null)
			return null;
		return encode(in.getBytes());
	}

	/**
	 * MD5校验(16位)
	 */
	public static String encode16(String in) throws CodecException {
		if (in == null)
			return null;
		String s = encode(in.getBytes());
		return s.substring(8, 24);
	}

}
